package cn.yunhe.validate;

import cn.yunhe.model.Transaction;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

/**
 * Created by zs on 2017/12/18.
 */
public class TransactionValidator implements Validator {

    public boolean supports(Class<?> aClass) {
        //判断验证的类型是否正确
        return Transaction.class.equals(aClass);
    }

    public void validate(Object target, Errors errors) {
        Transaction trans = (Transaction) target;
        //计算总价-单价*数量  的误差
        double dis = trans.getAmount() - (trans.getPrice() * trans.getQuantity());
        if (Math.abs(dis) > 0.1) {
            errors.rejectValue("amount", null, "误差过大，业务错误");
        }
    }
}
